package cn.iocoder.yudao.module.crm.service.statistics;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.tag.CrmStatisticsTagReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.tag.CrmStatisticsTagRespVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.label.CrmLabelDO;
import cn.iocoder.yudao.module.crm.dal.mysql.statistics.CrmStatisticsTagMapper;
import cn.iocoder.yudao.module.crm.service.label.CrmLabelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;


@Service
public class CrmStatisticsTagServiceImpl implements CrmStatisticsTagService {

    @Resource
    private CrmLabelService crmLabelService;

    @Resource
    private CrmStatisticsTagMapper crmStatisticsTagMapper;

    @Override
    public List<CrmStatisticsTagRespVO> getCustomerTagCount(CrmStatisticsTagReqVO reqVO) {
        List<CrmLabelDO> labelList = crmLabelService.getLabelListByModuleBySet(reqVO.getModuleId(), reqVO.getSetId());
        if (CollUtil.isEmpty(labelList)) {
            return Collections.emptyList();
        }
        List<CrmStatisticsTagRespVO> respVOList = crmStatisticsTagMapper.selectCustomerTagCount(convertList(labelList, CrmLabelDO::getId));
        if (CollUtil.isEmpty(respVOList)) {
            return Collections.emptyList();
        }

        CollectionUtil.sort(respVOList, Comparator.comparingLong(CrmStatisticsTagRespVO::getCount).reversed());
        // 转化map
        Map<Long, CrmLabelDO> labelMap = crmLabelService.getLabelMap(convertList(respVOList, CrmStatisticsTagRespVO::getLabelId));
        respVOList.forEach(data -> {
            MapUtils.findAndThen(labelMap, data.getLabelId(), d -> data.setName(d.getName()));
        });

        return respVOList;
    }

    @Override
    public List<CrmStatisticsTagRespVO> getClueTagCount(CrmStatisticsTagReqVO reqVO) {

        List<CrmLabelDO> labelList = crmLabelService.getLabelListByModuleBySet(reqVO.getModuleId(), reqVO.getSetId());
        if (CollUtil.isEmpty(labelList)) {
            return Collections.emptyList();
        }
        List<CrmStatisticsTagRespVO> respVOList = crmStatisticsTagMapper.selectClueTagCount(convertList(labelList, CrmLabelDO::getId));
        if (CollUtil.isEmpty(respVOList)) {
            return Collections.emptyList();
        }
        CollectionUtil.sort(respVOList, Comparator.comparingLong(CrmStatisticsTagRespVO::getCount).reversed());
        // 转化map
        Map<Long, CrmLabelDO> labelMap = crmLabelService.getLabelMap(convertList(respVOList, CrmStatisticsTagRespVO::getLabelId));
        respVOList.forEach(data -> {
            MapUtils.findAndThen(labelMap, data.getLabelId(), d -> data.setName(d.getName()));
        });

        return respVOList;
    }
}
